package com.fowo.api.datainterface.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.fowo.api.common.util.ContextHolder;
import com.fowo.api.datainterface.constants.DatainterfaceTypeKind;
import com.fowo.api.datainterface.model.DataContextModel;
import com.fowo.api.datainterface.service.DatainterfaceService;
import com.fowo.api.datainterface.service.DatainterfaceTaskService;
import com.fowo.api.datainterface.service.TaskJobService;
import com.fowo.api.datainterface.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Service
@Slf4j
@DS("ds24")
public class TaskJobServiceImpl implements TaskJobService {
    @Resource
    private DatainterfaceTaskService datainterfaceTaskService;
//    @Resource
//    private SQLManager sqlManager;

    private NamedParameterJdbcTemplate jdbcTemplate;
    public NamedParameterJdbcTemplate getJdbcTemplate() {
        if (jdbcTemplate == null) {
            jdbcTemplate = new NamedParameterJdbcTemplate(ContextHolder.getApplicationContext().getBean(DataSource.class));
        }
        return jdbcTemplate;
    }

    @Override
    public void runSyncDefault(JSONObject args) {
        String taskCode = JsonUtil.getJsonToBeanSafe(args, "['taskCode'][0]", String.class);
        Assert.notBlank(taskCode, "任务必须指定事务编号:taskCode");
        DataContextModel contextModel = DataContextModel.builder().build();
        contextModel.setTaskCode(taskCode);
        contextModel = datainterfaceTaskService.getToInfoByTaskCode(contextModel, DatainterfaceTypeKind.From.getValue(), null, null);
        datainterfaceTaskService.getToInfoByTaskCode(contextModel, DatainterfaceTypeKind.To.getValue(), null, null);
    }


//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void runSyncK3Cloud(JSONObject args) {
//        String taskCode = JsonUtil.getJsonToBeanSafe(args, "['taskCode'][0]", String.class);
//        String truncateTable = JsonUtil.getJsonToBeanSafe(args, "['truncateTable'][0]", String.class);
//        Assert.notBlank(taskCode, "任务必须指定事务编号:taskCode");
//        DataContextModel contextModel = DataContextModel.builder().build();
//        contextModel.setTaskCode(taskCode);
//        contextModel = datainterfaceTaskService.getToInfoByTaskCode(contextModel, DatainterfaceTypeKind.From.getValue(), null, null);
//        datainterfaceTaskService.getToInfoByTaskCode(contextModel, DatainterfaceTypeKind.To.getValue(), (to) -> {
//            if (StrUtil.isNotBlank(truncateTable)) {
////                sqlManager.executeUpdate(StrUtil.format("truncate table {}", truncateTable), null);
//                getJdbcTemplate().update(StrUtil.format("truncate table {}", truncateTable), new MapSqlParameterSource());
//            }
//            return null;
//        }, null);
//    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void runSyncK3Cloud(JSONObject args) {
        String encode = JsonUtil.getJsonToBeanSafe(args, "['encode'][0]", String.class);
        Assert.notBlank(encode, "接口编号不存在");
        Object o = SpringUtil.getBean(DatainterfaceService.class).executeInterface(null, encode, null, null, null);
    }
}
